﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib;
using DBConnectionLib;
using StructureMap;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    public class TestUserSettings
    {
        private const string strControlPath = BaseDirectoryHelper.Unused + "\\control";

        #region Ignored_Tests
        [Test]
        public void TestSingleTon()
        {
            Assert.IsTrue(UserSettings.Instance().InstanceFlag, "Could not create UserSettings Singleton");
//            Assert.Fail("not implemented yet");
        }
        [Test]
        [Ignore]
        public void TestSetDatabaseProject()
        {
            DatabaseProject oDbProject = new DatabaseProject(BaseDirectoryHelper.TestFiles_output, "TestUserSettings");
            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDbProject.AddNewDatabase(oStub.GetDatabaseObject());
            UserSettings.Instance().DatabaseProject = oDbProject;
            Assert.IsNotNull(UserSettings.Instance().DatabaseProject, "DatabaseProject should not be null");
        }

        [Test]
        [Ignore]
        public void TestLoadConfigFile()
        {
            
            // config file contains this:
            //ProjectBaseDirectory=S:\DBSourceTools_root\TestFiles\control\UserSettings
            //DefaultDatabase=DBSourceToolsTestDB

            // database project name == parsed config file name minus the ".dbsp".

            UserSettings.Instance().LoadLastProjectConfigFile(BaseDirectoryHelper.TestFiles_control_UserSettings + "\\" +
                "DBSourceToolsTestDB.dbsp");
            Assert.AreEqual("DBSourceToolsTestDB", UserSettings.Instance().DatabaseProject.ProjectName);
            Assert.AreEqual(BaseDirectoryHelper.TestFiles_control_UserSettings + "\\" +
                "DBSourceToolsTestDB.xml", UserSettings.Instance().DatabaseProject.ProjectFileName);
            Assert.AreEqual("DBSourceToolsTestDB", UserSettings.Instance().CurrentDbNickName);

        }

        [Test]
        [Ignore]
        public void TestSaveConfigFile()
        {
            DatabaseProject oDBProject = new DatabaseProject(
                BaseDirectoryHelper.TestFiles_output, "TestWriteConfig");
            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDatabaseObject());
            UserSettings.Instance().DatabaseProject = oDBProject;

            UserSettings.Instance().ProjectBaseDirectory = BaseDirectoryHelper.TestFiles_output;
            try
            {
                UserSettings.Instance().SaveConfig();
                Assert.Fail("SaveConfig should throw an exception");
            }
            catch (Exception ex)
            {
                // this should throw an exception
                if (!ex.ToString().Contains("LastConfigFile"))
                    Assert.Fail("SaveConfig should throw a LastConfigFile exception");
            }

            FileHelper oFileHelper = new FileHelper();
            oFileHelper.CompareFiles(BaseDirectoryHelper.TestFiles_control_UserSettings + "\\TestWriteConfig.dbsp", 
                UserSettings.Instance().LastProjectConfigFile);

        }

        [Test]
        [Ignore]
        public void TestCloseProject()
        {
            DatabaseProject oDBProject = new DatabaseProject(
                BaseDirectoryHelper.TestFiles_output, "TestWriteConfig");
            Stubs.NewDatabaseProjectFormStub oStub = new TestDBSourceToolsLibNUnit.Stubs.NewDatabaseProjectFormStub();
            oDBProject.AddNewDatabase(oStub.GetDatabaseObject());
            UserSettings.Instance().DatabaseProject = oDBProject;

            Assert.IsTrue(UserSettings.Instance().DatabaseProject.SourceDatabases.Count == 1, "Source db count should be 1");
            UserSettings.Instance().CloseProject();
            Assert.IsNull(UserSettings.Instance().DatabaseProject);
            Assert.IsTrue(String.IsNullOrEmpty(UserSettings.Instance().CurrentDbNickName), "Current db nick should be null");
            //Assert.IsTrue(UserSettings.Instance().DatabaseProject.SourceDatabases.Count == 0, "Source db count should be 0");

        }

        #endregion

        [Test]
        public void LoadDatabaseProject_WithInvalidSettings_UpgradesSettings()
        {
            ObjectFactory.Initialize(x => x.AddRegistry(new CommandLineRegistry()));

            string strProjectFile = Constants.AdventureWorks_ProjectFile;

            string projectName = "";
            string projectBaseDirectory = "";

            FileUtils.ParseProjectFileName(strProjectFile, out projectName, out projectBaseDirectory);

            Assert.AreEqual("AdventureWorks", projectName);
            Assert.AreEqual(@"E:\source\dbsourcetools\DBSourceTools_root\TestFiles\control\AdventureWorks", projectBaseDirectory);

            UserSettings.Instance().LoadLastProjectConfigFile(Constants.AdventureWorks_ProjectFile);

            Assert.IsTrue(UserSettings.Instance().ProjectNeedsSaving);

            SourceDatabase sourceDb = UserSettings.Instance().getSourceDatabase("AdventureWorksLT");
            Assert.IsTrue(sourceDb.NeedsWriteTargets);

        }

    }
}
